package medicalDocumentation.medicalDoc;

import entity.Disease;
import entity.DiseaseEntity;
import entity.MedicalCase;

import java.util.ArrayList;

import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateful
public class CaseDiseaseSessionBean implements CaseDiseaseSessionBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private MedicalCase medicalCase;
    private List<DiseaseEntity> diseaseEntityList = new ArrayList<>();
    ArrayList<DiseaseEntity> res;
    private DiseaseEntity diseaseEntity;

    public CaseDiseaseSessionBean() {
    }

    @Override
    public List<Disease> getDisease() {
        String strQuery = "SELECT d from Disease d";
        Query query = em.createQuery(strQuery);
//     System.out.println(query.getResultList().size());
        return query.getResultList();

    }

    @Override
    public void deleteMedicine(Long CIN, Long ID) {
        DiseaseEntity disease = em.find(DiseaseEntity.class, ID);
        MedicalCase mCase = em.find(MedicalCase.class, CIN);
        mCase.getDiseases().remove(disease);

        em.flush();
        em.refresh(disease);
        em.remove(disease);



    }

    @Override
    public ArrayList<DiseaseEntity> displayCaseDisease(Long CIN) {
        MedicalCase mCase = em.find(MedicalCase.class, CIN);
        res = new ArrayList();

        for (int i = 0; i < mCase.getDiseases().size(); i++) {
            res.add(mCase.getDiseases().get(i));

        }
        return res;
    }

    @Override
    public Long addDisease(Long CIN, String code, String name) {
//              System.out.println("Session bean ----- add Medicine called");
//              System.out.println("Session bean ----- add Medicine called code" + code);
        diseaseEntity = new DiseaseEntity(code, name);

//            res.add(diseaseEntity);
//          System.out.println("Session bean ----- add Medicine called" + medicineEntity.getMedicineName()+ medicineEntity.getCode()+ medicineEntity.getDose());
//      
//             System.out.println("Session bean ----- add Medicine called" +CIN);
        medicalCase = em.find(MedicalCase.class, CIN);

//             System.out.println("Session bean ----- add Medicine called" + medicalCase.getCIN());
//        System.out.println("Session bean ----- add Medicine called" + medicineEntity.getCode());

        medicalCase.getDiseases().add(diseaseEntity);
        //     medicalCase.setMedicines(medicineEntityList);
//             
        em.persist(diseaseEntity);

        em.persist(medicalCase);

        em.flush();
        return diseaseEntity.getId();
    }
    
    @Override
    public void deleteDisease(Long cin, Long id){
        medicalCase = em.find(MedicalCase.class, cin);
        DiseaseEntity de = em.find(DiseaseEntity.class, id);
        List<DiseaseEntity> dl = medicalCase.getDiseases();
        dl.remove(de);
        medicalCase.setDiseases(dl);
        em.remove(de);
        em.flush();
    }

    @Override
    public MedicalCase getMedicalCase() {
        return medicalCase;
    }

    @Override
    public void setMedicalCase(MedicalCase medicalCase) {
        this.medicalCase = medicalCase;
    }

    @Override
    public List<DiseaseEntity> getDiseaseEntityList() {
        return diseaseEntityList;
    }

    @Override
    public void setDiseaseEntityList(List<DiseaseEntity> diseaseEntityList) {
        this.diseaseEntityList = diseaseEntityList;
    }

    @Override
    public ArrayList<DiseaseEntity> getRes() {
        return res;
    }

    @Override
    public void setRes(ArrayList<DiseaseEntity> res) {
        this.res = res;
    }

    @Override
    public DiseaseEntity getDiseaseEntity() {
        return diseaseEntity;
    }

    @Override
    public void setDiseaseEntity(DiseaseEntity diseaseEntity) {
        this.diseaseEntity = diseaseEntity;
    }
}
